package com.wardrobe.soa.api.shop;

import com.google.gson.internal.LinkedTreeMap;
import com.wardrobe.domain.booking.BookingShowBean;
import com.wardrobe.domain.goods.GoodsShowBean;
import com.wardrobe.domain.shop.ShopSaveBean;
import com.wardrobe.domain.shop.ShopShowBean;
import com.wardrobe.soa.common.ApiResult;
import com.wardrobe.soa.utils.CommonConsant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Administrator on 2018/11/16.
 */
@RestController
@RequestMapping("/api/shop")
public class ShopController {
    private static final Logger logger = LoggerFactory.getLogger(ShopController.class);

    @Autowired
    private ShopService shopService;

    @RequestMapping("/join")
    public ApiResult joinShop(ShopSaveBean bean){
        try {
//            if(StringUtils.isNotBlank(shopJson)){
                int flag = shopService.saveShop(bean);
                if(flag == -1){
                    return new ApiResult(CommonConsant.bussinessFailedCode,CommonConsant.failed,"该用户已经入住过商家",flag);
                }
                return new ApiResult(flag == 0 ? CommonConsant.sqlExcetureFailedCode : CommonConsant.successCode, flag == 0 ? CommonConsant.failed : CommonConsant.success, flag == 0 ? CommonConsant.sqlExcetureFailedMsg : CommonConsant.successMsg,flag);
//            }
        }catch (Exception e){
            logger.error("joinShop",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/shopDetail")
    public ApiResult shopDetail(long id){
        try {
            ShopShowBean shopShowBean = shopService.shopDetail(id);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,shopShowBean);
        }catch (Exception e){
            logger.error("shopDetail",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }
    @RequestMapping("/getLngLat")
    public ApiResult getLngLat(String address){
        try {
            LinkedTreeMap map = shopService.getLngLat(address);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,map);
        }catch (Exception e){
            logger.error("getLngLat",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");

    }

    @RequestMapping("/getBookingListByShopId")
    public ApiResult getBookingListByShopId(long shopId){
        try {
            List<BookingShowBean> list = shopService.getBookingListByShopId(shopId);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,list);
        }catch (Exception e){
            logger.error("getBookingListByShopId",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }

    @RequestMapping("/getGoodsListByShopId")
    public ApiResult getGoodsListByShopId(long shopId){
        try {
            List<GoodsShowBean> list = shopService.getGoodsListByShopId(shopId);
            return new ApiResult(CommonConsant.successCode,CommonConsant.success,CommonConsant.successMsg,list);
        }catch (Exception e){
            logger.error("getGoodsListByShopId",e);
        }
        return new ApiResult(CommonConsant.serverFailedCode,CommonConsant.failed,CommonConsant.serverFailedMsg,"");
    }
}
